using System;
using System.Collections.Generic;
using System.Text;

using StateMachine_Graphical_Editor.Forms;
using KLATencor.CommonApp.Infrastructure.StateMachine;

namespace StateMachine_Graphical_Editor.Utils.Forms_RunTime_Managment
{
    /// <summary>
    /// A single form information and management class.
    /// <para>Each opened form is managed by a form-descriptor</para>
    /// </summary>
    class FormDescriptor
    {
        #region Members:

        /// <summary>
        /// The name is as the form's main state's name 
        /// </summary>
        private readonly string name;

        /// <summary>
        /// The form managed by this descriptor
        /// </summary>
        //private readonly SubStatesForm form;
        private readonly IFormManagment form;
        
        /// <summary>
        /// The descriptor's parent in forms' openning hierarchy tree
        /// </summary>
        private FormDescriptor parent; //null if its the machine itself;
        private IList<FormDescriptor> children;

        #endregion

        #region C'tor:

        public FormDescriptor(IFormManagment form)
        {
            this.name = form.getName();//MainState.Name;
            this.form = form;
            this.children = new List<FormDescriptor>();
        }

        #endregion

        #region Getters/Setters:

        public FormDescriptor Parent
        {
            get { return parent; }
            set { parent = value; }
        }

        public IFormManagment Form
        {
            get { return form; }
        }

        /// <summary>
        /// Gets the form's main state's name
        /// </summary>
        public string Name
        {
            get { return name; }
        }

        public IList<FormDescriptor> Children
        {
            get { return children; }
        }

        public State CurrentState
        {
            set { form.setCurrentState(value); }
        }

        public Transition LastTransition
        {
            set { form.setLastTransition(value); }
        }

        public bool PermitEventLauching
        {
            set { form.permitEventLaunching(value); }
        }

        #endregion

        #region Methods:

        /// <summary>
        /// Checks if the form with the name parameter is an offspring of this form
        /// </summary>
        /// <param name="formName"> the form's name to find</param>
        /// <returns>a form-descriptor of found form or null if not found</returns>
        public FormDescriptor Find(string formName)
        {
            if (this.name == formName)
                return this;
            else
            {
                foreach (FormDescriptor fd in this.children)
                {
                    FormDescriptor desc = fd.Find(formName);
                    if (desc != null)
                        return desc;
                }
                return null;
            }
        }

        /// <summary>
        /// Adds a child to this descriptor.
        /// <para>A descriptor child is added if its form is opened from 
        /// <para>this descriptor's form</para></para>
        /// </summary>
        /// <param name="child">the child to add</param>
        public void addChild(FormDescriptor child)
        {
            children.Add(child);
        }

        public void close()
        {
            form.closeForm();
        }

        public void show()
        {
            form.showForm();
        }

        public void focus()
        {
            form.activateForm();
        }
        #endregion

    }
}
